<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
(function(){
//****************************************************************************
// API

this.module = function(){
    var args = arguments, ns = args[0], t = typeof ns, expected = 2, obj;
    
    if (t == "object")
        obj = ns;
    else if (t == "string")
        obj = t && findNamespace(ns);        
    else if (t != "string")
        ns = "", expected = 1;

    if(args.length == expected)
        var init, main = args[expected-1];
    
    else if(args.length == expected+1)
        var init = args[expected-1], main = args[expected];
    
    else
        return undefined;

    var module = obj && findModule(obj);
    if (module)
    {
        var api = module.api;
        if (typeof main == "function")
        	main.apply(module.scope);
        else
            module.scope.module(main);        
    }
    else 
        var api = createModule(ns, init, main);
     
    return api;
}

var extend = this.extend = function(l, r)
{
	l = typeof l == "string" ? findNamespace(l) : l;
    var module = findModule(l);
    if (module)
    {
        if (typeof r == "function")
            r.apply(module.scope);
        else
            module.scope.extend(r);
        
        return module.api;
    }

    var obj = l.prototype || l;
    for (var n in r)
        obj[n] = r[n];
        
    return obj;
};

//****************************************************************************
// Modules

var modules = {};

function createModule(ns, init, main){
	var f = init && function(){return f.apply(_, arguments)},
	   API = f || function(){}, n = ns, p = n.split("::"), _;    
    
    if (ns && p.length == 2)
    {
        var inherit = p[0];
        n = p[1];
        if (inherit && inherit != "Object")
            API.prototype = new (findNamespace(inherit));
    }

    var Scope = main;
    Scope.prototype = new API;
    extend(Scope, {
        module: function(obj){ extend(Scope, obj); },
        extend: function(obj){ extendAPI(Scope, API, obj); }
    });

    var api = ns.indexOf("::") != -1 /*isClass*/ ? API : new API(); 
    var scope = _ = new Scope();

    var id = UID();
    var index = n || "Module_" + id;
    modules[index] = {
        id: id,
        ns: n,
        api: api, 
        API: API,
        scope: scope,
        Scope: Scope
    };    

    if (ns)
        createNamespace(ns, api);

    return api;
}

function findModule(api){
    var m;
    for(var n in modules)
        if ((m = modules[n]).api == api)
            return m;

    return undefined;
}

function extendAPI(Scope, API, obj)
{
    var s = Scope.prototype, a = API.prototype, f;
    for (var n in obj)
        a[n] = typeof (f = obj[n]) == "function" ?  bind(f, s) : f;

    return a;
};

function bind(f, _){return function(){return f.apply(_, arguments)}};

function UID(){return ++UID._};
UID._ = -1;

//****************************************************************************
// Namespaces

function createNamespace(ns, source) {
    if (!ns) return false;
    
    var n = ns.split("."), target = window, o;
    for (var i = 0, l = n.length-1, ni; (ni = n[i]) && (i < l); i++)
        target = (o = target[ni]) ? o : target[ni] = {};
    
    return target[ni] = source || {};
};

function findNamespace(ns) {
    if (!ns) return false;
    
    var n = ns.split("."), target = window, o;    
    for (var i = 0, l = n.length, ni; (ni = n[i]) && (i < l); i++)
        if (o = target[ni])
            target = o
        else
            return undefined;
    
    return target;
};

//****************************************************************************
})();

module("MOD", function(){

this.extend({
    show: function(){
        return "I can read " + this.inner() +
            " and " + hidden + " stuff.";
    }
});

var hidden = "hidden";

this.module({
    inner: function(){
        return "protected";
    }
});

});

/*
module("x", function(){this.module({	
})});

module("x", {
});

module("x", function(){this.extend({    
})});

extend("x", {
});
/**/


</script>
</head>
<body>
</body>
</html>